package com.jt.shop.controller;

import com.google.common.base.Preconditions;
import com.jt.shop.model.User;
import com.jt.shop.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by he on 2015/9/5.
 */
@Controller
public class LoginController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public String login() {
        return "user/login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String username, String password, HttpSession session) {
        Preconditions.checkNotNull(username);
        Preconditions.checkNotNull(password);
        User loginUser = userService.login(username, DigestUtils.md2Hex(password));
        session.setAttribute("loginUser", loginUser);
        return "redirect:/user/home";
    }

}
